package co.fitstart.mobile.web.controller.course;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.wechat.web.interceptor.WechatJsConfig;

import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.course.round.Goal;
import co.fitstart.entity.courseware.assessment.BasicData;
import co.fitstart.entity.courseware.assessment.PostureJoint;
import co.fitstart.entity.courseware.assessment.StudentHabit;
import co.fitstart.entity.user.User;
import co.fitstart.entity.user.basic.BodyDataTarget;
import co.fitstart.mobile.service.BasicInformationService;
import co.fitstart.mobile.service.CourseRoundService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;
import co.fitstart.mobile.web.interceptor.WechatAuthority;
import co.fitstart.mobile.web.resolver.Secure;

/**
 * 基础信息
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("/rounds/{courseRoundId}/basic-information")
public class BasicInformationController {
    
    @Autowired
    private CourseRoundService courseRoundService;
    
    @Autowired
    private BasicInformationService basicInformationService;
    
    /**
     * 目标展示页
     * @param user
     * @param courseRoundId
     * @return
     */
    @RequestMapping(value = "/target", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView target(@Secure User user,
                                @PathVariable("courseRoundId") Long courseRoundId) {
        ModelAndView modelAndView = new ModelAndView("course/basic/target");
        
        CourseRound courseRound = courseRoundService.loadCourseRound(courseRoundId);
        
        if(courseRound != null && !courseRound.isDisabled() && courseRound.getStudent().getUser() == user) {
            Goal goal = basicInformationService.loadGoal(courseRound);
            
            List<BodyDataTarget> targets = basicInformationService.listBodyDataTarget(courseRound);
            
            modelAndView.addObject("courseRoundId", courseRoundId);
            modelAndView.addObject("weeks", courseRound.getFitnessGroup().getWeeks());
            modelAndView.addObject("goal", goal);
            modelAndView.addObject("targets", CollectionUtils.constructMapValueFromList(targets, "bodyDataType.alias"));
        }
        
        return modelAndView;
    }
    
    /**
     * 学员评估
     * @param user
     * @param courseRoundId
     * @return
     */
    @RequestMapping(value = "/student-assessment", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView assessment(@Secure User user,
                                   @PathVariable("courseRoundId") Long courseRoundId) {
        ModelAndView modelAndView = new ModelAndView("course/evaluation/coach-evaluation");
        
        CourseRound courseRound = courseRoundService.loadCourseRound(courseRoundId);
        
        if(courseRound != null && !courseRound.isDisabled() && courseRound.getStudent().getUser() == user) {
        	 BasicData basicData = basicInformationService.loadBasicData(courseRound);
             StudentHabit habit = basicInformationService.loadStudentHabit(courseRound);
             PostureJoint postureJoint = basicInformationService.loadPostureJoint(courseRound);
            
             modelAndView.addObject("courseRoundId", courseRoundId);
             modelAndView.addObject("basicData", basicData);
             modelAndView.addObject("habit", habit);
             modelAndView.addObject("postureJoint", postureJoint);
        }
        
        return modelAndView;
    }
    
    /**
     * 学员作息/饮食/运动习惯
     * @param user
     * @param courseRoundId
     * @return
     */
    @RequestMapping(value = "/habit", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView habit(@Secure User user,
                              @PathVariable("courseRoundId") Long courseRoundId) {
        ModelAndView modelAndView = new ModelAndView("");
        
        CourseRound courseRound = courseRoundService.loadCourseRound(courseRoundId);
        
        if(courseRound != null && !courseRound.isDisabled() && courseRound.getStudent().getUser() == user) {
            StudentHabit habit = basicInformationService.loadStudentHabit(courseRound);
            
            modelAndView.addObject("habit", habit);
        }
        
        return modelAndView;
    }
    
    /**
     * 关节灵活性评估
     * @param user
     * @param courseRoundId
     * @return
     */
    @RequestMapping(value = "/joint-flexibility", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView jointFlexibility(@Secure User user,
                                         @PathVariable("courseRoundId") Long courseRoundId) {
        ModelAndView modelAndView = new ModelAndView("");
        
        CourseRound courseRound = courseRoundService.loadCourseRound(courseRoundId);
        
        if(courseRound != null && !courseRound.isDisabled() && courseRound.getStudent().getUser() == user) {
            PostureJoint postureJoint = basicInformationService.loadPostureJoint(courseRound);
            
            if(postureJoint != null) {
                modelAndView.addObject("postureSummary", postureJoint.getPostureSummary());
                modelAndView.addObject("jointSummary", postureJoint.getJointSummary());
                modelAndView.addObject("summary", postureJoint.getSummary());
            }
        }
        
        return modelAndView;
    }
}
